package com.slg.controller;

import com.slg.entity.JsonResponse;
import com.slg.entity.Result;
import com.slg.entity.dto.EmployeeDto;
import com.slg.entity.dto.SalaryDto;
import com.slg.entity.pojo.Employee;
import com.slg.entity.pojo.Page;
import com.slg.entity.pojo.Salary;
import com.slg.entity.pojo.Shenqing;
import com.slg.service.AdminiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminiService adminiService;
    //1.1用户管理操作
    @GetMapping("/queryEmployee")
    public JsonResponse queryEmployee(@RequestParam (required = false,defaultValue = "1")Integer pageNum,
                                      @RequestParam (required = false,defaultValue = "6")Integer pageSize,Employee employee
                                      ){
        Page<Employee> employeePage = adminiService.queryEmployee(pageNum,pageSize,employee);
        return JsonResponse.success(employeePage);
    }

    //    @GetMapping("/queryDatabase1")
//    public  JsonResponse queryDatabase1(){
//        List<Shenqing> shenqing = adminiService.queryDatabase1();
//        return JsonResponse.success(shenqing);
//    }
    @GetMapping("/queryDatabase1")
    public JsonResponse queryDatabase1(@RequestParam (required = false,defaultValue = "1")Integer pageNum,
                                       @RequestParam (required = false,defaultValue = "6")Integer pageSize
    ){
        Page<Shenqing> shenqingPage = adminiService.queryDatabase1(pageNum,pageSize);
        return JsonResponse.success(shenqingPage);
    }

    //1.2工资类型管理
    @GetMapping("/querySalary")
    public JsonResponse querySalary(@RequestParam (required = false,defaultValue = "1") Integer pageNum,
                                    @RequestParam (required = false,defaultValue = "1000") Integer pageSize, Salary salary
                                    ){
        Page<Salary> salaryPage = adminiService.querySalary(pageNum,pageSize,salary);
        return JsonResponse.success(salaryPage);
    }
    //2.2工资信息管理
    @GetMapping("/querySalaryDto")
    public JsonResponse querySalaryDto(@RequestParam (required = false,defaultValue = "1") Integer pageNum,
                                    @RequestParam (required = false,defaultValue = "1000") Integer pageSize, Salary salary
    ){
        Page<SalaryDto> salaryPage = adminiService.querySalaryDto(pageNum,pageSize,salary);
        return JsonResponse.success(salaryPage);
    }

    @GetMapping("/login")
    public JsonResponse login(Employee employee){
        Employee login = adminiService.login(employee);
        if(login!=null){
            return JsonResponse.success(login);
        }
        return JsonResponse.error();
    }
//    @GetMapping("/queryByUsername")
//    public  JsonResponse queryByUsername(String name){
//        SalaryDto salaryDto =  adminiService.queryByUsername(name);
//        return JsonResponse.success(salaryDto);
//    }
    //3.2报销申请
    @PutMapping("/setBaoXiao/{baoxiao}/{id}")
    public  JsonResponse setBaoXiao(@PathVariable Integer baoxiao,@PathVariable Integer id){
        int i= adminiService.setBaoXiao(baoxiao,id);
        if(i>0)return JsonResponse.success(i);
        return  JsonResponse.error(Result.UPDATE_ERROR.code, Result.ERROR.msg, "申请失败");
    }
    //3.1查询本人信息
    @GetMapping("/querySalaryByOne")
    public JsonResponse querySalaryByOne(Integer id){
        Salary salary =  adminiService.querySalaryByOne(id);
        if(salary != null) return JsonResponse.success(salary);
        return JsonResponse.error();
    }

    @GetMapping("/queryDatabase")
    public  JsonResponse queryDatabase(){
        List<Salary> salary1 = adminiService.queryDatabase();
        return JsonResponse.success(salary1);
    }


    //3.3异常数据申诉
//@PutMapping("/updateException/{username}/{position}/{basicsalary}/{jiangjin}/{baoxiao}/{id}")
//    public JsonResponse updateException(@PathVariable String username,@PathVariable String position,@PathVariable Integer basicsalary,
//          @PathVariable Integer jiangjin,@PathVariable Integer baoxiao,@PathVariable Integer id){
//        int i= adminiService.updateException(username,position,basicsalary,jiangjin,baoxiao,id);
//            if(i>0)return JsonResponse.success(i);
//        return  JsonResponse.error();
//    }


    @PostMapping("/updateException")
    public JsonResponse updateException(@RequestBody Shenqing salaryDto){
        int i= adminiService.updateException(salaryDto);
        if(i>0)
            return JsonResponse.success(null);
        return  JsonResponse.error();
    }

    @PostMapping("/updateEmployee")
    public JsonResponse updateEmployee(@RequestBody Employee employee){
        int i= adminiService.updateEmployee(employee);
        if(i>0)
            return JsonResponse.success(null);
        return  JsonResponse.error();
    }

    @DeleteMapping("/deleteEmployee/{id}")
    public JsonResponse deleteEmployee(@PathVariable int id){
             int i=adminiService.deleteEmployee(id);
             if(i>0)
             return JsonResponse.success(null);
             return JsonResponse.error(Result.UPDATE_ERROR.code, Result.UPDATE_ERROR.msg, "删除失败");
    }
    @DeleteMapping("/deleteEmployee1/{id}")
    public JsonResponse deleteEmployee1(@PathVariable int id){
        int i=adminiService.deleteEmployee1(id);
        if(i>0)
            return JsonResponse.success(null);
        return JsonResponse.error(Result.UPDATE_ERROR.code, Result.UPDATE_ERROR.msg, "删除失败");
    }
    @DeleteMapping("/jujube/{id}")
    public JsonResponse jujube(@PathVariable int id){
        int i=adminiService.jujube(id);
        if(i>0)
            return JsonResponse.success(null);
        return  JsonResponse.error();
    }

    @PostMapping("/updateSalary1")
    public JsonResponse updateSalary1(@RequestBody SalaryDto salaryDto){
        int i= adminiService.updateSalary1(salaryDto);
        if(i>0)
            return JsonResponse.success(null);
        return  JsonResponse.error();
    }
    @PostMapping("/add")
    public  JsonResponse add(@RequestBody Employee employee){

       int i= adminiService.add(employee);
       if(i>0)
           return JsonResponse.success(null);
       return JsonResponse.error();
    }
    @PostMapping("/add1")
    public  JsonResponse add1(@RequestBody SalaryDto salaryDto){

        int i= adminiService.add1(salaryDto);
        if(i>0)
            return JsonResponse.success(null);
        return JsonResponse.error();
    }
        @PutMapping("/updateSalary3/{id}")
    public  JsonResponse updateSalary3(@PathVariable int id){
        int i=adminiService.updateSalary3(id);
            if(i>0)
                return JsonResponse.success(null);
            return JsonResponse.error();
        }


}
